<template>
  <div class="authorization-container relative">
    <div class="authorization-body">
      <!-- 账号密码登录 -->
      <div class="account-login-warp">
        <a-form-model
          ref="accountLoginForm"
          :model="accountLoginForm"
          :rules="accountLoginRules"
        >
          <!-- 账号 -->
          <a-form-model-item ref="account" prop="account">
            <a-input
              v-model="accountLoginForm.account"
              placeholder="请输入账号"
              size="large"
              ref="accountLoginForm.account"
            />
          </a-form-model-item>
          <!-- 密码 -->
          <a-form-model-item ref="password" prop="password">
            <a-input-password
              v-model="accountLoginForm.password"
              placeholder="请输入密码"
              size="large"
              ref="accountLoginForm.password"
            />
          </a-form-model-item>
        </a-form-model>
      </div>
      <div class="login-footer">
        <a-button
          type="primary"
          size="large"
          @click="login()"
          :loading="loading"
        >
          登录</a-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { getAuthData } from "@/api/auth.js";
import { saveData, clearStorageData } from "@/tools";
export default {
  data() {
    return {
      loading: false,
      //账号密码登录
      accountLoginForm: {
        //jtsuper
        account: "",
        //123456
        password: "",
      },
      accountLoginRules: {
        account: [
          {
            required: false,
            message: "您输入的账号不正确",
            trigger: ["change"],
          },
        ],
        password: [
          {
            required: false,
            message: "您输入的密码不正确",
            trigger: ["change"],
          },
        ],
      },
    };
  },
  mounted() {
    clearStorageData();
  },
  methods: {
    //登录
    login() {
      this.loading = true;
      return new Promise((resolve, reject) => {
        let { account, password } = this.accountLoginForm;
        let fd = new FormData();
        fd.append("username", account);
        fd.append("password", password);
        fd.append("grant_type", "password");
        getAuthData(fd)
          .then((res) => {
            let { data } = res;
            this.saveLoginData(data);
            this.getTenantSource();
            resolve();
          })
          .catch((_) => {
            this.loading = false;
            clearStorageData();
            reject();
          })
          .finally((_) => {});
      });
    },

    //查询租户信息列表
    getTenantSource() {
      return new Promise((resolve, reject) => {
        this.$store
          .dispatch("queryTenantData")
          .then((source) => {
            if (source.length == 0) {
              //没有租户信息-拒绝进入系统
              this.$message.warning(`很抱歉该账号下无任何项目信息`);
            } else {
              // 只有一个租户信息
              if (source.length == 0) {
                saveData("TENANT", source[0] || {});
                saveData("ORGANIZATION", source[0] || {});
              }
              //进入系统
              this.$router.push({
                path: "/",
                replace: true,
              });
            }
            resolve();
          })
          .catch((_) => {
            reject();
          })
          .finally((_) => {
            this.loading = false;
          });
      });
    },

    //存储相关信息
    saveLoginData(data = {}) {
      let { userInfo, access_token } = data;
      saveData("LOGIN_DATA", data);
      saveData("USER_INFO", userInfo ?? {});
      saveData("ACCESS_TOKEN", access_token ?? "");
      saveData("PAGE_REFRESH", 1);
    },
  },
};
</script>

<style lang="less" scoped>
.authorization-container {
  width: 100%;
  height: 100%;
  background: url("~@/assets/images/loginBg.png");
  background-size: 100% 100%;
  overflow: hidden;
  .authorization-body {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: no-repeat center 35px;
    background-size: 215px 78px;
    border-radius: 8px;
    width: 580px;
    height: 526px;
    box-sizing: border-box;
    padding: 150px 60px 0 60px;
    background: rgba(255, 255, 255, 1) url("~@/assets/images/logo_1.png") center
      80px no-repeat;
    //账号密码登录
    .account-login-warp {
      padding: 40px 0 50px 0;
      .ant-input-affix-wrapper {
        width: 100%;
      }
    }
    .login-footer {
      width: 100%;
      flex: 1;
      .ant-btn {
        display: block;
        width: 100%;
      }
      .login-tip-warp {
        text-align: center;
        padding-top: 26px;
        span {
          color: @primary-color;
          cursor: pointer;
          &:hover {
            text-decoration: underline;
          }
        }
      }
    }
  }
  //视频背景
  .video-bg-warp {
    width: 100%;
    height: 100%;
    video {
      width: 100%;
      height: 100%;
      object-fit: fill;
    }
  }
}
</style>
